// 开始打印
doPrint(){
	const _this = this;
	
	//已选中打印机
	var paired = uni.getStorageSync("printerSelected");
	if(paired==null || paired=="" || paired==undefined){
		showToastTip("请先点击更多设置蓝牙")	;
	}else{
		uni.showLoading({
			title:  "正在获取打印数据"
		})
		// const textData = this.createPrintText(this.printLang);
		var textData = "";
		if(paired.name=="IposPrinter"){
			textData = this.createPrintTextBJ(this.printLang);
		}else{
			textData = this.createPrintText(this.printLang);
		}
		var reqUrl = this.url+"getBarCode";
		getRespDataWithUrl(reqUrl,'post',{
			id:_this.printInfo.printCacheId,
			screenCutCount:paired.name=="IposPrinter" ? '1' : (paired.name=="InnerPrinter" || paired.name=="V2" ? '2' : '0')
		},function(res){
			if(res.code==0){
				const bytesData = res.data.data;						
				var bytes = hexToBytes(bytesData); 
				uni.showLoading({
					title: "正在打印中..."
				})
				var data = printTicket(textData,bytes);
				uni.hideLoading();
				if(data.code==0){//打印成功
					var reqUrl = _this.url+"upPrintCount";//修改打印次数
					getRespDataWithUrl(reqUrl,"post",{
						userId:_this.userId,
						id:_this.printInfo.printCacheId
					},function(res){
						if(res.code==0){
							_this.printInfo.printCount = _this.printInfo.printCount+1;
						}else{
							showToastTip(res.msg)						
						}
					});	
				}else{
					if(data.code==-1){
						showToastTip("打印失败")	
					}else if(data.code==1){
						showToastTip("请先点击更多设置蓝牙")	
					}else if(data.code==2){
						showToastTip("请先打开蓝牙")	
					}
					//showToastTip(data.msg)	
				}
			}
		})
	}				
},

createPrintTextBJ(lang){
	const that = this;
	var emptyLine = this.fillSpace(0,'',16)+"\r\n";
	var saleName = uni.getStorageSync("saleName");
	var content = "";
	var headerStr = this.fillSpace(1,this.name+" 第("+this.printInfo.printCount+")次打印",16)+"\r\n";
	content = content+headerStr;
	content = content+emptyLine;
	content = content+this.fillSpace(1,"时间:"+this.printInfo.printTime,31)+"\r\n";
	if(saleName!=null && saleName!="" && saleName!=undefined){
		content = content+this.fillSpace(1,"店名:"+saleName,16)+"\r\n";
	}
	content = content+this.fillSpace(1,"会员:"+this.uname,16)+"\r\n";
	content = content+emptyLine;
	content = content+ fillChars("*",16)+"\r\n";
	content = content+emptyLine;
	content = content+this.fillSpace(1,"货号/品名",5)+this.fillSpace(1,"单价",5)+this.fillSpace(1,"数量",5)+"\r\n";
	content = content+emptyLine;
	this.printInfo.dataList.forEach((item,idx)=>{
		var codes = that.getCodeName(item.typeId,"-",item.buyCodes);
		var plv = item.param3;
		var bm = item.buyMoney;
		//content = content+this.fillSpace(1,codes.toUpperCase(),10)+this.fillSpace(1,plv,10)+this.fillSpace(1,bm,9)+"\r\n";
		// if(codes.length > 6){
			content = content+this.fillSpace(1,codes,16)+"\r\n";
			content = content+this.fillSpace(1,"",5)+this.fillSpace(1,plv,5)+this.fillSpace(1,bm,5)+"\r\n";
			// content = content+printThreeData("",plv,bm+'\n',this.printLang);
		// }else{
			// content = content+this.fillSpace(1,codes.toUpperCase(),5)+this.fillSpace(1,plv,5)+this.fillSpace(1,bm,5)+"\r\n";
			// content = content+printThreeData(codes,plv,bm+'\n',this.printLang);
		// }
	}); 
	content = content+emptyLine;
	content = content+ fillChars("*",16)+"\r\n";
	content = content+emptyLine;
	var num = "数量:"+this.printInfo.dataList.length+" 总金额:"+this.printInfo.totalMoney;
	content = content+this.fillSpace(1,num,31)+"\r\n";
	content = content+emptyLine;
	content = content+ fillChars("*",16)+"\r\n";
	content = content+emptyLine;
	content = content+this.fillSpace(1,"3天内有效",16)+"\r\n";
	if(this.pager.pages>1){
		content = content+this.fillSpace(1,"第"+this.pager.currpage+"/"+this.pager.pages+"页",16)+"\r\n";
	}
	content = content+emptyLine;
	return content;
},

getStrLen(str){
	var realLength = 0, len = str.length, charCode = -1;
      for (var i = 0; i < len; i++) {
        charCode = str.charCodeAt(i);
		if(this.printLang=='lao'){
			realLength += 1;
		}else{
			if (charCode >= 0 && charCode <= 128)
	          realLength += 1;
	        else
	          realLength += 2;
		}			       
      }
      return realLength;
},

fillSpace(loc,content,totalLen){//填充空格
	var clen = this.getStrLen(content);
	var newContent = "";
	if(totalLen>clen){
		newContent = content;
		for(var i=0;i<(totalLen-clen);i++){
			if(loc==0){//左填充空格
				newContent = " "+newContent;
			}else if(loc==1){//右边填充空格
				newContent = newContent+" ";
			}
		}
		return newContent;
	}else{
		return content.substr(0,totalLen+1);
	}
}

createPrintText(lang){
	const that = this;
	var emptyLine = this.fillSpace(0,'',31)+"\r\n";
	var saleName = uni.getStorageSync("saleName");
	if(lang == 'zh'){
		var content = "";
		var headerStr = this.fillSpace(1,this.name+" 第("+this.printInfo.printCount+")次打印",31)+"\r\n";
		content = content+headerStr;
		content = content+emptyLine;
		content = content+this.fillSpace(1,"时间:"+this.printInfo.printTime,31)+"\r\n";
		if(saleName!=null && saleName!="" && saleName!=undefined){
			content = content+this.fillSpace(1,"店名:"+saleName,31)+"\r\n";
		}
		content = content+this.fillSpace(1,"会员:"+this.uname,31)+"\r\n";
		//content = content+emptyLine;				
		content = content+ fillChars("*",31)+"\r\n";
		// content = content+this.fillSpace(1,"货号/品名",10)+this.fillSpace(1,"单价",10)+this.fillSpace(1,"数量",9)+"\r\n";
		content = content+printThreeData("货号/品名","单价","数量\n",this.printLang)
		
		this.printInfo.dataList.forEach((item,idx)=>{
			var codes = that.getCodeName(item.typeId,"-",item.buyCodes);
			var plv = item.param3;
			var bm = item.buyMoney;
			//content = content+this.fillSpace(1,codes.toUpperCase(),10)+this.fillSpace(1,plv,10)+this.fillSpace(1,bm,9)+"\r\n";
			if(codes.length > 6){
				content = content+this.fillSpace(1,codes,31)+"\r\n";
				content = content+printThreeData("",plv,bm+'\n',this.printLang);
			}else{
				content = content+printThreeData(codes,plv,bm+'\n',this.printLang);
			}
		}); 
		content = content+ fillChars("*",31)+"\r\n";	
		var num = "数量:"+this.printInfo.dataList.length+" 总金额:"+this.printInfo.totalMoney;
		content = content+this.fillSpace(1,num,31)+"\r\n";
		content = content+ fillChars("*",31)+"\r\n";				
		content = content+this.fillSpace(1,"3天内有效",31)+"\r\n";
		if(this.pager.pages>1){
			content = content+this.fillSpace(1,"第"+this.pager.currpage+"/"+this.pager.pages+"页",31)+"\r\n";
		}
		content = content+emptyLine;
		return content;
	}
},